namespace testWeek {
    // 1. 定义商品接口
    interface ProductInterface {
        id: number
        name: string
        price: number
        category: Category
        getDetails(): string
    }

    // 2. 枚举类型 Category
    enum Category {
        Electronics = 'Electronics',
        Clothing = 'Clothing',
        Food = 'Food',
    }

    // 3. 创建 Product 类
    class Product implements ProductInterface {
        id: number;
        name: string;
        price: number;
        category: Category;

        // _stock 表示库存
        private _stock: number;
        // 税率
        static taxRate: number = 0.1;

        constructor(id: number, name: string, price: number, category: Category, _stock: number) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.category = category;
            this._stock = _stock
        }

        getDetails(): string {
            return `Product [id:${this.id},name:${this.name},price:${this.price},category:${this.category}, _stock:${this._stock}]`
        }

        // 判断商品是否有库存
        private isInStock(): boolean {
            return this._stock > 0;
        }

        get stock(): number {
            return this._stock;
        }

        set stock(value: number) {
            if (value < 0) {
                throw new Error("库存不能为空");
            }
            this._stock = value;
        }


        // 税费
        static calculateTax(price: number) {
            return price * this.taxRate;
        }
    }

    // 4. 创建 ElectronicProduct 子类
    class ElectronicProduct extends Product {
        // 保修期
        warranty: number;
        constructor(id: number, name: string, price: number, category: Category, _stock: number, warranty: number) {
            super(id, name, price, category, _stock),
                this.warranty = warranty
        }
        // 重写getDetails()方法，增加保修期信息
        getDetails(): string {
            return `${super.getDetails()},warranty:${this.warranty} 年`
        }

    }

    // 5. 定义泛型接口 约束商品的折扣信息
    interface Discountable<T> {
        applyDiscount(discount: T): number
    }

    // 使用泛型接口约束 Product 类型
    class DiscountedProduct extends Product implements Discountable<number | string> {
        applyDiscount(discount: string | number): any {
            if (typeof discount === 'number') {
                this.price -= discount
            } else if (typeof discount === 'string') {
                const discountValue = parseFloat(discount)
                if (!isNaN(discountValue)) {
                    this.price -= discountValue
                }
            }
        }
    }

    // 6.定义函数类型 促销策略
    type PromotionFunction = (product: Product) => number


    console.log('----------------------------------------');


    // 示例函数；应用促销策略
    const summerSale: PromotionFunction = (product: Product) => {
        return product.price * 0.9; // 打九折
    }

    // 测试用例
    const laptop = new ElectronicProduct(1, 'Laptop', 1000, Category.Clothing, 10, 2)
    console.log(laptop.getDetails()); // 输出商品详情
    console.log('Price after tax:', Product.calculateTax(laptop.price)); // 计算税费

    laptop.stock = 5
    console.log('Stock:', laptop.stock);  // 获取库存

    const discountedLaptop = new DiscountedProduct(2, 'Laptop', 1000, Category.Electronics, 10)
    discountedLaptop.applyDiscount('100'); // 应用折扣
    console.log('折扣后的价格:', discountedLaptop.price); // 折扣后的价格





}
